<?php
namespace app\index\controller;
use think\Controller;
use think\Session;

class util extends Controller {
    
    // 地震信息 消息
    public function sendEarthInfo($longitude, $latitude, $magnitude, $depth, $time) {
        Session::delete('code');
        $url = 'http://192.168.18.240:5000/';
        $post_data = array(
            "type" => "earthquakeInfo",
            "timestamp" => time(),
            // "timestamp" => 1490028880000,
            "message" => array(
                "code" => "CD".$time,  
                "longitude" => $longitude,
                "latitude" => $latitude,
                "magnitude" => $magnitude,
                "depth" => $depth,
                "time" => $time,
                "version" => 1
            )
        );
        Session::set('code',$time);
        $this->playReq($url, $post_data);
    }

    // 地震日志消息
    public function sendLogInfo($info) {
        $time = Session::get('code');
        $url = 'http://192.168.18.240:5000/';
        $post_data = array(
            "type" => "logger",
            "timestamp" => time(),
            "message" => array(
                "code" => "CD".$time,
                "level" => "INFO",
                "data" => $info,
                "version" => 1
            )
        );
        $this->playReq($url, $post_data);
    }

    private function playReq($url, $post_data) {
        //初始化
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_data));
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        // var_dump($data);
        return $data=="1" ? "yes" : "no";
    }
}